package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.Episode;
import com.elinkway.infinitemovies.view.SquareLayout;
import com.le123.ysdq.R;

import java.util.ArrayList;

/**
 * @author liuliwei
 * 
 */
public class EpisodeGridAdapter extends RecyclerView.Adapter<EpisodeGridAdapter.ViewHolder> {

	private Context mContext;
	private ArrayList<Episode> episodes;
	private LayoutInflater inflater;
	private boolean isDialog;
	private int mCurrentPlayPosition;
	private OnItemClickListener mListener;

	public class ViewHolder extends RecyclerView.ViewHolder {
		public ViewHolder(View arg0) {
			super(arg0);

			tv = (TextView) arg0
					.findViewById(R.id.episode_item_num);
			ispay = (ImageView) arg0
					.findViewById(R.id.ispay);
		}

		TextView tv;
		ImageView ispay;
		SquareLayout sl;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
	{
		View view = inflater.inflate(R.layout.episode_grid_item,
				viewGroup, false);
		ViewHolder viewHolder = new ViewHolder(view);
		return viewHolder;
	}

	@Override
	public void onBindViewHolder(final ViewHolder viewHolder, final int i)
	{
		viewHolder.tv.setText(episodes.get(i).getPorder());

		if (mCurrentPlayPosition == i) {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_3599f8));
			viewHolder.tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.rectangle_round_corner_3_background));
			TextPaint paint = viewHolder.tv.getPaint();
			paint.setFakeBoldText(true);
		} else {
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_666666));
			viewHolder.tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.rectangle_round_corner_3_no_stroke));
			TextPaint paint = viewHolder.tv.getPaint();
			paint.setFakeBoldText(false);
		}

		if ("1".equals(episodes.get(i).getIsPay())){
			viewHolder.ispay.setVisibility(View.VISIBLE);
			viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.color_bebebe));
		}else{
			viewHolder.ispay.setVisibility(View.GONE);
		}

		if (mListener != null){
			viewHolder.tv.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v){
					if(i < episodes.size()){
						mListener.onItemClick(viewHolder.itemView, i, episodes.get(i).getPorder());
					}
				}
			});
		}

	}

	public EpisodeGridAdapter(Context mContext, ArrayList<Episode> episodes, boolean isDialog) {

		this.mContext = mContext;
		this.episodes = episodes;
		this.isDialog = isDialog;
		this.inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getItemCount() {
		return episodes.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setEpisodes(ArrayList<Episode> episodes) {
		this.episodes=episodes;
		notifyDataSetChanged();
	}

	public void setSelectPosition(int position) {
		if (position == mCurrentPlayPosition) {
			return;
		}
		mCurrentPlayPosition = position;
		notifyDataSetChanged();
	}

	public ArrayList<Episode> getEpisodes() {
		return episodes;
	}

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		mListener = onItemClickListener;
	}

	public interface OnItemClickListener {
		void onItemClick(View view, final int i, String porder);
	}

}
